﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace homework2
{
    public class ShapeFactory
    {
        public enum Type { Square, Rectangle, Triangle };

        static Random random = new Random();

        static int[] edgesNumber = { 1, 2, 3 };
        public static IShape CreateShape(Type type, double[] edges)
        {
            if (edges == null)
                throw new InvalidOperationException("edges cannot be null");
            int index = Convert.ToInt32(type);
            if (edgesNumber[index] != edges.Length)
                throw new InvalidOperationException("invlid edge array number");

            IShape shape = null;
            switch (type)
            {
                case Type.Square:
                    shape = new Square(edges[0]);
                    break;
                case Type.Rectangle:
                    shape = new Rectangle(edges[0], edges[1]);
                    break;
                case Type.Triangle:
                    shape = new Triangle(edges[0], edges[1], edges[2]);
                    break;
                default:
                    throw new InvalidOperationException("invalid shape type");
            }

            if (!shape.isValid())
            {
                throw new InvalidOperationException("invalid edges input");
            }

            return shape;
        }

        public static IShape CreateRandomShape()
        {
            int index = random.Next(0, 3);
            Type type = (Type)Enum.Parse(typeof(Type), index.ToString());
            double[] edges = new double[edgesNumber[index]];
            IShape product = null;
            do
            {
                try
                {
                    for (int i = 0; i < edges.Length; i++)
                    {
                        edges[i] = random.Next(100);
                    }
                    product = CreateShape(type, edges);
                }
                catch { }
            } while (product == null);

            return product;
        }
    }
}
